<?php

namespace app\index\controller;

use think\Db;


class User extends Base
{


    public function newwithdraw()
    {

        $uid = $this->uid;;
        $user = Db::name('userinfo')->where('uid', $uid)->find();
        $this->assign('user', $user);
        return $this->fetch('newwithdraw');
    }

    public function cashlist()
    {


        $where['uid'] = $this->uid;;
        $where['bptype'] = 0;

        $list = Db::name('balance')->where($where)->order('bpid desc')->paginate(20);
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function ercode()
    {
        $uid = $this->uid;
        $user = Db::name('userinfo')->where('uid', $uid)->find();
        //推广二维码
        if ($user['otype'] == 101) {
            $oid = $uid;
        } else {
            $oid = $user['oid'];
        }
        $oid_url = "http://" . $_SERVER['SERVER_NAME'] . '?fid=' . $oid;

        $this->assign('oid_url', $oid_url);
        return $this->fetch();
    }

    public function about(){
        return $this->fetch();
    }

    public function kefu(){
        $config = $this->conf;
        return $this->redirect($config['kf_url']);
    }

    /**
     * 充值记录
     * @author lukui  2017-07-24
     * @return [type] [description]
     */
    public function reglist()
    {
        $where['uid'] = $this->uid;;
        $where['bptype'] = 1;
        $list = Db::name('balance')->where($where)->order('bpid desc')->paginate(20);
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function pay()
    {
        return $this->fetch();
    }

    public function pay3()
    {
        $rc = Db::name('rcset')->where('type', 'usdt')->find();
        $this->assign($rc);
        return $this->fetch();
    }

    public function userre()
    {
        $post = input('post.');
        if (!$post) {
            $this->error(lang('parameterError'));
        }
        $post['pay_type'] = 1;
        $post['bpprice'] = $post['t_num'];
        if (!$post['pay_type'] || !$post['bpprice']) {
            return WPreturn(lang('parameterError'), -1);
        }


        $uid = $this->uid;
        $user = $this->user;
        $nowtime = time();

        //插入充值数据
        $data['bptype'] = 1;
        $data['bptime'] = $nowtime;
        $data['bpprice'] = $post['bpprice'];
        $data['remarks'] = lang('userRecharge');
        $data['uid'] = $uid;
        $data['isverified'] = 0;
        $data['btime'] = $nowtime;
        $data['reg_par'] = 0;
        $data['balance_sn'] = $uid . $nowtime . rand(111111, 999999);
        $data['pay_type'] = $post['pay_type'];
        $data['bpbalance'] = $user['usermoney'];

        $ids = db('balance')->insertGetId($data);
        if (!$ids) {
            return WPreturn(lang('systemError'), -1);
        }
        return WPreturn(lang('successfully'), 1);
    }

    /**
     * 用户个人中心首页
     * @author lukui  2017-07-21
     * @return [type] [description]
     */
    public function index()
    {
        $uid = $this->uid;;


        $user = Db::name('userinfo')->where('uid', $uid)->find();
        $maphold = array('uid' => $uid, 'ostaus' => 0);
        $maphold['selltime'] = array('gt', time());

        $hold['count'] = Db::name('order')->where($maphold)->order('oid desc')->count();
        $hold['sum'] = Db::name('order')->where($maphold)->order('oid desc')->sum('fee');
        $hold['sum'] = empty($hold['sum']) ? 0 : $hold['sum'];
        $this->assign('hold', $hold);

        //出金------------------------------------------
        //银行卡
        $data['banks'] = db('banks')->select();

        //地区
        $province = db('area')->where(array('pid' => 0))->select();

        //已签约信息
        $data['mybank'] = db('bankcard')->alias('b')->field('b.*,ba.bank_nm')
            ->join('__BANKS__ ba', 'ba.id=b.bankno')
            ->where('uid', $uid)->find();


        //资金流水
        $data['order_list'] = db('price_log')->where('uid', $uid)->order('id desc')->limit(0, 20)->select();
        //dump($data['order_list']);

        //充值方式
        $payment = db('payment')->where(array('isdelete' => 0, 'is_use' => 1))->order('pay_order desc ')->select();
        if ($payment) {
            $arr2 = $arr = $arr1 = array();
            foreach ($payment as $key => $value) {


                $arr1 = explode('|', trimall($value['pay_conf']));

                foreach ($arr1 as $k => $v) {
                    $arr2 = explode(':', trimall($v));
                    if (isset($arr2[0]) && isset($arr2[1])) {
                        $arr[$arr2[0]] = $arr2[1];
                    }


                }
                $payment[$key]['pay_conf_arr'] = $arr;


            }
        }

        //推广二维码
        if ($user['otype'] == 101) {
            $oid = $uid;
        } else {
            $oid = $user['oid'];
        }
        $data['oid_url'] = "http://" . $_SERVER['SERVER_NAME'] . '?fid=' . $oid;

        //dump($payment);exit;
        $data['sub_bankno'] = substr($data['mybank']['accntno'], -4, 4);

        $this->assign('province', $province);
        $this->assign($data);
        $this->assign('payment', $payment);

        $qcode = Db::name('qcode')->find(1);
        $this->assign('qcode', $qcode);

        $payall = Db::name('qcode')->where('id != 1')->select();

        $tixian = Db::name('config')->find(24);
        $gonggao = Db::name('config')->find(25);
        $appurl = Db::name('config')->find(26);
        $purl = Db::name('config')->find(27);
        $time = $tixian['value'];
        $pos = strpos($time, '-');
        $begin = substr($time, 0, $pos);
        $begin = date('H', strtotime($begin));

        $end = substr($time, $pos + 1);
        $end = date('H', strtotime($end));
        $nowtime = date('H', time());

        if ($nowtime >= $begin && $nowtime <= $end) {
            $this->assign('notallow', 1);
        } else {
            $this->assign('notallow', 2);
        }
        $this->assign('begin', $begin);
        $this->assign('end', $end);
        $this->assign('tixian', $tixian);
        $this->assign('gonggao', $gonggao);
        $this->assign('purl', $purl);
        $this->assign('appurl', $appurl);
        $this->assign('payall', $payall);
        return $this->fetch();
    }


    /**
     * 现金充值
     * @author lukui  2017-07-21
     * @return [type] [description]
     */
    public function recharge()
    {
        if (input('post.')) {
            $data = input('post.');
            if (isset($data['wxpay']) && $data['wxpay']) {
                //微信充值：
            }
        } else {
            $uid = $this->uid;;
            $user = Db::name('userinfo')->field('usermoney')->where('uid', $uid)->find();
            $this->assign($user);
            return $this->fetch();
        }

    }


    /**
     * 用户提现
     * @author lukui  2017-07-21
     * @return [type] [description]
     */
    public function cash()
    {
        $uid = $this->uid;
        if (input('post.')) {
            $data = input('post.');

            if ($data) {
                if (!$data['price']) {
                    return WPreturn(lang('pleaseEnterTheWithdrawalAmount'), -1);
                }
                //验证申请金额
                $user = $this->user;
                if ($user['ustatus'] != 0) {
                    return WPreturn(lang('youHaveNoRightToPay'), -1);
                }
                $conf = $this->conf;


                if ($conf['is_cash'] != 1) {
                    return WPreturn(lang('temporarilyUnableToPay'), -1);
                }
                if ($conf['cash_min'] > $data['price']) {
                    return WPreturn(lang('theMinimumAmountOfSingleWithdrawalIs') . $conf['cash_min'], -1);
                }
                if ($conf['cash_max'] < $data['price']) {
                    return WPreturn(lang('theMaximumAmountOfSingleWithdrawalIs') . $conf['cash_max'], -1);
                }

                $_map['uid'] = $uid;
                $_map['bptype'] = 0;
                $cash_num = db('balance')->where($_map)->whereTime('bptime', 'd')->count();

                if ($cash_num + 1 > $conf['day_cash']) {
                    return WPreturn(lang('theMaximumNumberOfWithdrawalsPerDayIs') . $conf['day_cash'] . '次', -1);
                }
                $cash_day_max = db('balance')->where($_map)->whereTime('bptime', 'd')->sum('bpprice');
                if ($conf['cash_day_max'] < $cash_day_max + $data['price']) {
                    return WPreturn(lang('theMaximumNumberOfWithdrawalsAmountOfTheDayIs') . $conf['cash_day_max'], -1);
                }


                //代理商们是否有欠费
                $myoids = myupoid($this->user['oid']);

                $can_cash = 1;

                if ($myoids) {
                    foreach ($myoids as $k => $v) {
                        if ($v['usermoney'] - $v['minprice'] < 0) {
                            $can_cash = 0;
                            continue;
                        }
                    }
                }

                if (!$can_cash) {
                    return WPreturn(lang('systemError'), -1);
                }

                //代理商的话判断金额是否够
                if ($this->user['otype'] == 101) {
                    if (($this->user['usermoney'] - $data['price']) < $this->user['minprice']) {
                        return WPreturn(lang('yourDepositIs') . $this->user['minprice'] . lang('theBalanceAfterWithdrawalShallNotBeLessThanTheDeposit'), -1);
                    }
                }

                if ($this->user['otype'] == 0) {
                    if (($this->user['usermoney'] - $data['price']) < 0) {
                        return WPreturn(lang('theMaximumWithdrawalAmountIs') . $this->user['usermoney'] . lang('element'), -1);
                    }
                }

                if (($this->user['usermoney'] - $data['price']) < 0) {
                    return WPreturn(lang('theMaximumWithdrawalAmountIs') . $this->user['usermoney'] . lang('element'), -1);
                }


                //签约信息
                $mybank = db('bankcard')->where('uid', $uid)->find();


                //提现申请
                $newdata['bpprice'] = $data['price'];
                $newdata['bptime'] = time();
                $newdata['bptype'] = 0;
                $newdata['remarks'] = lang('withdrawalByMembers');
                $newdata['uid'] = $uid;
                $newdata['isverified'] = 0;
                $newdata['bpbalance'] = 0;
                $newdata['bankid'] = $mybank['id'];
                $newdata['btime'] = time();
                $newdata['reg_par'] = $conf['reg_par'];


                $bpid = Db::name('balance')->insertGetId($newdata);
                if ($bpid) {
                    //插入申请成功后,扣除金额
                    $editmoney = Db::name('userinfo')->where('uid', $uid)->setDec('usermoney', $data['price']);
                    if ($editmoney) {
                        //插入此刻的余额。
                        $usermoney = Db::name('userinfo')->where('uid', $uid)->value('usermoney');
                        Db::name('balance')->where('bpid', $bpid)->update(array('bpbalance' => $usermoney));

                        //资金日志
                        set_price_log($uid, 2, $data['price'], lang('cashWithdrawal'), lang('withdrawalApplication'), $bpid, $usermoney);

                        return WPreturn(lang('withdrawalApplicationSubmittedSuccessfully'), 1);
                    } else {
                        //扣除金额失败，删除提现记录
                        Db::name('balance')->where('bpid', $bpid)->delete();
                        return WPreturn(lang('withdrawalFailure'), -1);
                    }

                } else {
                    return WPreturn(lang('withdrawalFailure'), -1);
                }


            } else {
                return WPreturn(lang('doNotPayThisWithdrawalTypeTemporarily'), -1);
            }
        } else {

            $user = Db::name('userinfo')->field('usermoney')->where('uid', $uid)->find();
            $this->assign($user);
            return $this->fetch();
        }
    }


    /**
     * 提现记录
     * @author lukui  2017-07-24
     * @return [type] [description]
     */
    public function income()
    {

        $where['uid'] = $this->uid;;
        $where['bptype'] = 0;

        $list = Db::name('balance')->where($where)->order('bpid desc')->paginate(20);

        $this->assign('list', $list);
        return $this->fetch();
    }


    /**
     * 用户资金明细
     * @author lukui  2017-07-21
     * @return [type] [description]
     */
    public function orders()
    {
        $uid = $this->uid;;
        $where['uid'] = $uid;
        $where['ostaus'] = 1;
        if (input('param.month')) {
            $month = input('param.month');
        } else {
            $month = date("m");
        }
        if (input('param.years')) {
            $years = input('param.years');
        } else {
            $years = date("Y");
        }

        //当月时间戳
        $BeginDate = date('Y-m-d', strtotime($years . '-' . $month . '-01'));
        $EndDate = date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));
        $BeginDate = strtotime($BeginDate);
        $EndDate = strtotime($EndDate);


        $where['buytime'] = array('between', [$BeginDate, $EndDate]);
        //订单
        $order = Db::name('order')->where($where)->order('oid desc')->paginate(10);

        if (input('get.page')) {  //ajax请求的

            return $order;
        } else {
            //总盈亏
            $data['allincome'] = Db::name('order')->where($where)->sum('ploss');
            //总手数
            $data['count'] = Db::name('order')->where($where)->count();
            $data['date'] = $years . '-' . $month;

            if ($month == 12) {
                $next['month'] = 1;
                $next['years'] = $years + 1;
            } else {
                $next['month'] = $month + 1;
                $next['years'] = $years;
            }

            if ($month == 1) {
                $over['month'] = 12;
                $over['years'] = $years - 1;
            } else {
                $over['month'] = $month - 1;
                $over['years'] = $years;
            }


            $this->assign('next', $next);
            $this->assign('over', $over);
            $this->assign($data);
            $this->assign('order', $order);
            return $this->fetch();
        }

    }


    /**
     * 用户积分
     * @author lukui  2017-07-21
     * @return [type] [description]
     */
    public function integral()
    {
        $uid = $this->uid;;
        $point = Db::name('userinfo')->where('uid', $uid)->value('userpoint');
        //进入是否签到
        $isregister = Db::name('integral')->where(array('uid' => $uid, 'type' => 1))->whereTime('time', 'd')->select();

        $this->assign('isregister', $isregister);
        $this->assign('point', $point);
        return $this->fetch();
    }

    /**
     * 签到处理
     * @author lukui  2017-07-21
     * @return [type] [description]
     */
    public function dointegral()
    {
        $uid = $this->uid;;
        //是否签到
        $isregister = Db::name('integral')->where(array('uid' => $uid, 'type' => 1))->whereTime('time', 'd')->select();
        if (empty($isregister)) { //签到
            //积分流水表 并增加积分
            $i_data['type'] = 1;
            $i_data['amount'] = 50;
            $i_data['time'] = time();
            $i_data['uid'] = $uid;
            $add = Db::name('integral')->insert($i_data);
            //会员增加积分
            Db::name('userinfo')->where('uid', $uid)->setInc('userpoint', $i_data['amount']);
            if ($add) {
                return WPreturn(lang('signInSucceeded'), 1);
            } else {
                return WPreturn(lang('checkInFailed'), -1);
            }
        } else {
            return WPreturn(lang('youHaveSignedInToday'), -1);
        }
    }


    /**
     * 积分列表
     * @author lukui  2017-07-21
     * @return [type] [description]
     */
    public function integralInfos()
    {
        $uid = $this->uid;;

        $integral = Db::name('integral')->where('uid', $uid)->order('id desc')->paginate(20);

        if (input('get.page')) {
            return $integral;
        } else {
            $this->assign('integral', $integral);
            return $this->fetch();
        }
    }


    /**
     * 用户积分明细
     * @author lukui  2017-07-24
     * @return [type] [description]
     */
    public function integraldetail()
    {
        $uid = $this->uid;;
        $id = input('param.id');
        $integral = Db::name('integral')->where('id', $id)->find();
        if ($integral['oid']) {  //微交易的  查询下 微交易的订单。
            $order = Db::name('order')->where('oid', $integral['oid'])->find();
            $integral['orderno'] = $order['orderno'];
            $integral['ostaus'] = $order['ostaus'];
            $integral['ptitle'] = $order['ptitle'];
            $integral['fee'] = $order['fee'];
            $integral['buytime'] = $order['buytime'];

        }
        $this->assign($integral);
        return $this->fetch();
    }


    /**
     * 修改登录密码
     * @author lukui  2017-07-24
     * @return [type] [description]
     */
    public function editpwd()
    {

        $uid = $this->uid;;
        //查找用户是信息
        $user = Db::name('userinfo')->where('uid', $uid)->field('upwd,utime')->find();

        //添加密码
        if (input('post.')) {
            $data = input('post.');
            if (!isset($data['oldpwd']) || empty($data['oldpwd'])) {
                return WPreturn(lang('pleaseEnterTheOriginalPassword'), -1);
            }
            //验证密码
            if ($user['upwd'] != md5($data['oldpwd'] . $user['utime'])) {
                return WPreturn(lang('theOriginalPasswordIsWrong'), -1);
            }
            if (!isset($data['newpwd']) || empty($data['newpwd'])) {
                return WPreturn(lang('pleaseEnterANewLoginPassword'), -1);
            }
            if (!isset($data['newpwd2']) || empty($data['newpwd2'])) {
                return WPreturn(lang('pleaseConfirmTheNewLoginPassword'), -1);
            }
            if ($data['newpwd'] != $data['newpwd2']) {
                return WPreturn(lang('twoDifferentPasswords'), -1);
            }
            if ($data['oldpwd'] == $data['newpwd']) {
                return WPreturn(lang('pleaseDoNotChangeToTheOriginalPassword'), -1);
            }

            $adddata['upwd'] = trim($data['newpwd']);
            $adddata['upwd'] = md5($adddata['upwd'] . $user['utime']);
            $adddata['uid'] = $uid;

            $newids = Db::name('userinfo')->update($adddata);
            if ($newids) {
                return WPreturn(lang('modificationSucceeded'), 1);
            } else {
                return WPreturn(lang('modificationFailedPleaseTryAgain'), -1);
            }

        }


        return $this->fetch();

    }


    /**
     * 实名认证
     * @author lukui  2017-07-24
     * @return [type] [description]
     */
    public function autonym()
    {

        return $this->fetch();
    }


    /**
     * 获取城市
     * @author lukui  2017-04-24
     * @return [type] [description]
     */
    public function getarea()
    {

        $id = input('id');
        if (!$id) {
            return false;
        }

        $list = db('area')->where('pid', $id)->select();
        $data = '<option value="">' . lang('select') . '</option>';
        foreach ($list as $k => $v) {
            $data .= '<option value="' . $v['id'] . '">' . $v['name'] . '</option>';
        }
        echo $data;

    }


    /**
     * 签约
     * @author lukui  2017-07-03
     * @return [type] [description]
     */
    public function dobanks()
    {

        $post = input('post.');

        foreach ($post as $k => $v) {

            if (empty($v)) {
                return WPreturn(lang('pleaseFillInTheInformationCorrectly'), -1);
            }

            $post[$k] = trim($v);

        }


        if (isset($post['id']) && !empty($post['id'])) {

            $ids = db('bankcard')->update($post);
        } else {
            unset($post['id']);
            $post['uid'] = $this->uid;
            $ids = db('bankcard')->insert($post);
        }

        if ($ids) {
            return WPreturn(lang('operationSuccessful'), 1);
        } else {
            return WPreturn(lang('operationFailedPleaseTryAgain'), -1);
        }


    }


    public function ajax_price_list()
    {
        $uid = $this->uid;

        $list = db('price_log')->where('uid', $uid)->order('id desc')->paginate(20);
        return $list;

    }


    public function addbalance()
    {


        $post = input('post.');
        if (!$post) {
            $this->error(lang('parameterError'));
        }

        if (!$post['pay_type'] || !$post['bpprice']) {
            return WPreturn(lang('parameterError'), -1);
        }


        $uid = $this->uid;
        $user = $this->user;
        $nowtime = time();

        //插入充值数据
        $data['bptype'] = 3;
        $data['bptime'] = $nowtime;
        $data['bpprice'] = $post['bpprice'];
        $data['remarks'] = lang('userRecharge');
        $data['uid'] = $uid;
        $data['isverified'] = 0;
        $data['btime'] = $nowtime;
        $data['reg_par'] = 0;
        $data['balance_sn'] = $uid . $nowtime . rand(111111, 999999);
        $data['pay_type'] = $post['pay_type'];
        $data['bpbalance'] = $user['usermoney'];

        $ids = db('balance')->insertGetId($data);
        if (!$ids) {
            return WPreturn(lang('systemError'), -1);
        }
        $data['bpid'] = $ids;
        $Pay = controller('Pay');
        //支付类型
        if ($post['pay_type'] == 'wxpay') {
            $res = $Pay->wxpay($data);
            return $res;
        }
        if ($post['pay_type'] == 'zypay') {
            $res = $Pay->zypay($data);
            return $res;
        }
        if ($post['pay_type'] == 'qbt_pay_wxpay') {
            $res = $Pay->qianbaotong($data, 1007);
            return $res;
        }
        if ($post['pay_type'] == 'qbt_pay_wxpay_code') {
            $res = $Pay->qianbaotong($data, 1004);
            return $res;
        }
        if ($post['pay_type'] == 'alipay') {
            $res = $Pay->alipay($data);

            return $res;
        }
        if ($post['pay_type'] == 'qtb_alipay') {
            $res = $Pay->qianbaotong($data, 1006);

            return $res;
        }
        if ($post['pay_type'] == 'qtb_yinlian') {
            $res = $Pay->qianbaotong($data, 1005);

            return $res;
        }
        if ($post['pay_type'] == 'izpay_wx') {
            $res = $Pay->izpay_wx($data);

            return $res;
        }
        if ($post['pay_type'] == 'izpay_alipay') {
            $res = $Pay->izpay_alipay($data);

            return $res;
        }


        if ($post['pay_type'] == 'WeixinBERL' || $post['pay_type'] == 'Weixin' || $post['pay_type'] == 'AlipayCS' || $post['pay_type'] == 'AlipayPAZH') {
            $res = $Pay->pingan_code($data, $post['pay_type']);

            return $res;
        }
        if ($post['pay_type'] == 'hf_codewx') {
            $res = $Pay->hf_codewx($data);
            return $res;
        }
        if ($post['pay_type'] == 'hf_codezfb') {
            $res = $Pay->hf_codezfb($data);
            return $res;
        }
        if ($post['pay_type'] == 'zhpay') {
            $paytype = $post['paytype'];
            $bankcode = $post['bankcode'];
            $res = $Pay->zhpay($data, $paytype, $bankcode);

            return $res;
        }


        //dump($data);qianbaotong


    }


}
